package intro.databases.gtCarRental.ui;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class RentACar extends GTCRActivity {
	Spinner  location, chooseBy, option;
	EditText pickupDate, pickupTime, returnDate, returnTime;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.rent_car);

		pickupDate = (EditText) findViewById(R.id.editText_rentCar_pickUpDate);
		pickupTime = (EditText) findViewById(R.id.editText_rentCar_pickUpTime);
		returnDate = (EditText) findViewById(R.id.editText_rentCar_returnDate);
		returnTime = (EditText) findViewById(R.id.editText_rentCar_returnTime);

		location = (Spinner) findViewById(R.id.spinner_rentCar_location);
		chooseBy = (Spinner) findViewById(R.id.spinner_rentCar_chooseBy);
		option = (Spinner) findViewById(R.id.spinner_rentCar_option);

		ArrayAdapter<CharSequence> lcAdapter = ArrayAdapter.createFromResource(this, R.array.gtcr_location, android.R.layout.simple_spinner_item);
		lcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		location.setAdapter(lcAdapter);

		ArrayAdapter<CharSequence> cbAdapter = ArrayAdapter.createFromResource(this, R.array.carSearch_chooseBy, android.R.layout.simple_spinner_item);
		cbAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		chooseBy.setAdapter(cbAdapter);
		chooseBy.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				TextView t = (TextView) arg1;
				if(t.getText().toString().equals("Choose by Type")) {
					ArrayAdapter<CharSequence> oAdapter = ArrayAdapter.createFromResource(RentACar.this, R.array.gtcr_type, android.R.layout.simple_spinner_item);
					oAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					option.setAdapter(oAdapter);
				} else {
					ArrayAdapter<CharSequence> oAdapter = ArrayAdapter.createFromResource(RentACar.this, R.array.gtcr_model, android.R.layout.simple_spinner_item);
					oAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
					option.setAdapter(oAdapter);
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	}
	
	public void search(View v) {
		ArrayList<String> carSearchValues = new ArrayList<String>();
		carSearchValues.add(pickupDate.getText().toString());
		carSearchValues.add(convertToHHMM(pickupTime.getText().toString()));
		carSearchValues.add(returnDate.getText().toString());
		carSearchValues.add(convertToHHMM(returnTime.getText().toString()));
		carSearchValues.add(((TextView)location.getSelectedView()).getText().toString());
		carSearchValues.add((((TextView)chooseBy.getSelectedView()).getText().toString()).equals("Choose by Type") ? "Type" : "CarModel");
		carSearchValues.add(((TextView)option.getSelectedView()).getText().toString());
		carSearchValues.add(pickupTime.getText().toString());
		carSearchValues.add(returnTime.getText().toString());
		
		Intent intent = new Intent(RentACar.this, CarAvailability.class);
		intent.putStringArrayListExtra("carSearchValues", carSearchValues);
		startActivity(intent);
	}

	private String convertToHHMM(String time) {
		String[] splits = time.split(":");
		int hours = Integer.parseInt(splits[0]);
		
		String[] splits2 = splits[1].split(" ");
		int minutes = Integer.parseInt(splits2[0]);
		
		if(splits2[1].equals("PM") && (hours != 12))
			hours+=12;
		
		return (hours<10) ? ("0"+hours) : hours +":"+minutes;
	}
}